package cn.ylz.ylmall.account.service.impl;

import cn.ylz.ylmall.account.dao.persist.repository.IPermissionRepository;
import cn.ylz.ylmall.account.pojo.entity.Permission;
import cn.ylz.ylmall.account.pojo.param.PermissionAddnewParam;
import cn.ylz.ylmall.account.pojo.param.PermissionUpdateParam;
import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.pojo.vo.PermissionListVO;
import cn.ylz.ylmall.account.pojo.vo.PermissionStandardVO;
import cn.ylz.ylmall.account.service.IPermissionService;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Value("${ylmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IPermissionRepository permissionRepository;
    @Override
    public void addnew(PermissionAddnewParam addnewParam) {
        int count = permissionRepository.countByName(addnewParam.getName());
        if (count > 0) {
            String message = "添加权限失败，权限名称已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(addnewParam,permission);
        count = permissionRepository.insert(permission);
        if (count != 1) {
            String message = "添加权限失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        PermissionStandardVO queryResult = permissionRepository.getStandardById(id);
        if (queryResult == null){
            String message = "删除权限失败,该权限不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int count = permissionRepository.delete(id);
        if (count != 1) {
            String message = "删除权限失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

    }

    @Override
    public void update(Long id, PermissionUpdateParam param) {
        PermissionStandardVO queryResult = permissionRepository.getStandardById(id);
        if (queryResult == null){
            String message = "修改权限失败,该权限不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(param,permission);
        permission.setId(id);
        int count = permissionRepository.update(permission);
        if (count != 1) {
            String message = "修改权限失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

    }

    @Override
    public PermissionStandardVO getPermissionStandard(Long id) {
        return permissionRepository.getStandardById(id);
    }

    @Override
    public PageData<PermissionListVO> list(Integer pageNum) {
        PageData<PermissionListVO> pageData = permissionRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<PermissionListVO> list(Integer pageNum, Integer pageSize) {
        PageData<PermissionListVO> pageData = permissionRepository.list(pageNum, pageSize);
        return pageData;
    }


}
